package com.yxdj.service.controller;

import com.yxdj.common.core.domain.R;
import com.yxdj.common.core.web.controller.BaseController;
import com.yxdj.common.core.web.domain.AjaxResult;
import com.yxdj.common.core.web.page.TableDataInfo;
import com.yxdj.common.log.annotation.Log;
import com.yxdj.common.log.enums.BusinessType;
import com.yxdj.common.security.annotation.InnerAuth;
import com.yxdj.common.security.annotation.RequiresPermissions;
import com.yxdj.service.api.domain.SubCategory;
import com.yxdj.service.service.SubCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 子分类管理
 */
@Tag(name = "子分类管理")
@RestController
@RequestMapping("/sub")
public class SubCategoryController extends BaseController {

    @Autowired
    private SubCategoryService subCategoryService;


    @Operation(summary = "查询所有子分类")
    @GetMapping("/all")
    public AjaxResult getAll() {
        return success(subCategoryService.list());
    }

    /**
     * 分页获取子分类列表
     */
    @Operation(summary = "分页获取子分类列表")
    @GetMapping("/list")
    public TableDataInfo list(SubCategory subCategory) {
        startPage();
        List<SubCategory> list = subCategoryService.getPageList(subCategory);
        return getDataTable(list);
    }

    /**
     * 获取子分类详细信息
     */
    @Operation(summary = "获取子分类详细信息")
    @GetMapping(value = "/getById/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(subCategoryService.getById(id));
    }

    /**
     * 新增子分类
     */
    @Operation(summary = "新增子分类")
    @Log(title = "子分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SubCategory subCategory) {
        return toAjax(subCategoryService.save(subCategory));
    }

    /**
     * 修改子分类
     */
    @Operation(summary = "修改子分类")
    @Log(title = "子分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SubCategory subCategory) {
        return toAjax(subCategoryService.updateById(subCategory));
    }

    /**
     * 删除子分类
     */
    @Operation(summary = "删除子分类")
    @RequiresPermissions("system:subCategory:remove")
    @Log(title = "子分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(subCategoryService.removeByIds(ids));
    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用获取前几个子分类的方法")
    @GetMapping("/app/getSubCategoryList/{limit}")
    public R<List<SubCategory> > getSubCategoryList(@PathVariable(name = "limit") Integer limit){
        List<SubCategory> subCategoryList = subCategoryService.getSubCategoryList(limit);
        return R.ok(subCategoryList);
    }
}
